//
//  CustomButtonStyle.swift
//  SwiftUIButton
//
//  Created by 李煜 on 2024/7/30.
//

import SwiftUI

struct CustomButtonStyle: View {
    var body: some View {
        Button(action: {}, label: {
            Label(
                title: { Text("Share")
                    .font(.title)
                    .fontWeight(.semibold)
                },
                icon: { Image(systemName: "square.and.arrow.up")
                    .font(.title)
                }
            )
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .background(LinearGradient(colors: [.darkGreen, .lightGreen], startPoint: .leading, endPoint: .trailing))
            .cornerRadius(40)
            .foregroundColor(.white)
            .padding(.horizontal, 20)
        })
        Button(action: {}, label: {
            Label(
                title: {
                    Text("Eidt")
                        .font(.title)
                        .fontWeight(.semibold)
                },
                icon: {
                    Image(systemName: "square.and.pencil")
                        .font(.title)
                }
            )
            .frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, maxWidth: .infinity)
            .padding()
            .background(LinearGradient(colors: [.darkGreen, .lightGreen], startPoint: .leading, endPoint: .trailing))
            .cornerRadius(40)
            .foregroundColor(.white)
            .padding(.horizontal, 20)
        })

        Button(action: {}, label: {
            Label(
                title: { Text("Delete")
                    .font(.title)
                    .fontWeight(.semibold)
                },
                icon: { Image(systemName: "trash")
                    .font(.title)
                }
            )
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .background(LinearGradient(colors: [.darkGreen, .lightGreen], startPoint: .leading, endPoint: .trailing))
            .cornerRadius(40)
            .foregroundColor(.white)
            .padding(.horizontal, 20)
        })

        // MARK: - 使用自定义按钮样式

        Button(action: {}, label: {
            Label(
                title: {
                    Text("自定义样式")
                        .font(.title)
                        .fontWeight(.semibold)
                },
                icon: {
                    Image(systemName: "trash")
                        .font(.title)
                }
            )
        })
        // 使用自定义按钮样式 创建同样的样式的按钮
        .buttonStyle(MyButtonStyle())

        // MARK: - 作业 点击时旋转的按钮

        Button(action: {}, label: {
            Label(
                title: {
                },
                icon: {
                    Image(systemName: "plus")
                        .font(.title)
                }
            )
        })
        .buttonStyle(MyRotationButtonStyle())
    }
}

// 创建自定义按钮样式
struct MyButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .frame(minWidth: /*@START_MENU_TOKEN@*/0/*@END_MENU_TOKEN@*/, maxWidth: .infinity)
            .padding()
            .background(LinearGradient(colors: [.darkGreen, .lightGreen], startPoint: .leading, endPoint: .trailing))
            .cornerRadius(40)
            .padding(.horizontal, 20)
            .foregroundColor(.white)
            // 点击按钮时缩小,松手放大
            .scaleEffect(configuration.isPressed ? 0.9 : 1.0)
    }
}

// 作业: 使用自定义样式创建一个按钮:点击时旋转
struct MyRotationButtonStyle: ButtonStyle {
    func makeBody(configuration: Configuration) -> some View {
        configuration.label
            .padding()
            .background(.purple)
            .foregroundColor(.white)
            .cornerRadius(40)
            .rotationEffect(configuration.isPressed ? .degrees(45) : .degrees(0))
    }
}

#Preview {
    CustomButtonStyle()
}
